# coding:utf-8

'''
三角数，正数的前 n 项之和
第1个三角数是 1
第2个三角数是 1 + 2 = 3
第3个三角数是 1 + 2 + 3 = 6
.....
第n个三角数是 1 + 2 + ... + n = (n-1) * n / 2
'''
import  datetime

def  get_triangular_number():
    divisor_set = set()
    n = 1
    even = 1
    odd = 1

    while(True):

        if (n %2 ==0):
            even = n
        else:
            even = n + 1

        if (n %2 ==0):
            odd = n + 1
        else:
            odd = n

        divisor_list1 = get_num_divisor(int(even/2))
        divisor_list2 = get_num_divisor(odd)
        # for i in divisor_list1:
        #     for j in divisor_list2:
        #         divisor_set.add(i * j)


        n += 1
        divisor_num = len(divisor_list1) * len(divisor_list2)
        if(divisor_num > 500):
            break


        divisor_set = set()

    return int(even / 2) * odd


def get_num_divisor(n):
    divisor_list = []
    #for i in range(1,int(n/2)+1)
    for i in range(1,int(n/2)+1):
        if(n % i) == 0:
            divisor_list.append(i)

    divisor_list.append(n)
    return divisor_list

def main():

    time_list = []
    for i in range(0,10):
        starttime = datetime.datetime.now()
        triangular_number = get_triangular_number()
        endtime = datetime.datetime.now()

        time_list.append((endtime - starttime).microseconds)

    print("The average time a program runs : ",float(sum(time_list) / len(time_list)))

    print("triangular_number: ", triangular_number)

if __name__ == '__main__':
    main()